package date0703pm;

import java.sql.Connection;
import java.sql.DriverManager;

public class JdbcTest18 {

	public static void main(String[] args) throws Exception {
		
		String driverClassName = "oracle.jdbc.driver.OracleDriver"  ;
		String url = "jdbc:oracle:thin:@localhost:1521:ecut" ;
		String user = "ecuter" ;
		String password = "ecuter" ;
		
//		String driverClassName = "com.mysql.cj.jdbc.Driver"  ;
//		String url = "jdbc:mysql://localhost:3306/ecut?serverTimezone=Asia/Shanghai&useSSL=false" ;
//		String user = "ecuter" ;
//		String password = "ecuter" ;
		
		// 1、加载数据库驱动
		Class.forName( driverClassName ); // ClassNotFoundException
		
		// 2、获得数据库连接
		Connection connection = DriverManager.getConnection( url , user , password ); // SQLException
		
		// Connection 接口中定义了 五个 表示事务隔离级别的常量:
		// public static final int TRANSACTION_NONE = 0 ;
		// public static final int TRANSACTION_READ_UNCOMMITTED = 1 ; // 0001 
		// public static final int TRANSACTION_READ_COMMITTED = 2 ; // 0010
		// public static final int TRANSACTION_REPEATABLE_READ = 4 ; // 0100
		// public static final int TRANSACTION_SERIALIZABLE = 8 ; // 1000
		int isolationLevel = connection.getTransactionIsolation() ;
		System.out.println( "transaction isolation level : " + isolationLevel );
		
		boolean autocommit = connection.getAutoCommit() ;
		System.out.println( "autocommit : " + autocommit );
		
		// setAutoCommit 用来设置提交模式: true 表示自动提交，false 表示手动提交
		connection.setAutoCommit( false );
		// 设置隔离级别 ( 建议 参数 使用 Connection 接口中定义的常量 )
		connection.setTransactionIsolation( Connection.TRANSACTION_SERIALIZABLE );
		
		System.out.println( "transaction isolation level : " + connection.getTransactionIsolation() );
		System.out.println( "autocommit : " + connection.getAutoCommit() );
		
		// 6、释放资源( 所有已经开启的资源必须都关闭 )
		connection.close(); // SQLException

	}

}
